package cn.itcast.blog.web.demo;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.itcast.blog.domain.Post;

/*
 * 使用注解拦截请求
 */
@WebServlet(urlPatterns = "/getAjax")
public class AjaxServlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//获取传入的参数
		String parameter = req.getParameter("id");
		System.out.println(parameter);
		
		Post post = new Post();
		post.setId(1);
		post.setTitle("java");
		post.setContent("java");
		post.setCreated(new Date());
		
		String jsonString = JSON.toJSONString(post);
		
		System.out.println(jsonString);
		
		resp.setContentType("application/json:charset=utf-8");
		
		resp.getWriter().write(jsonString);
		
	}
	
}
